<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改特色美食')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-meishi-edit" th:object="${tMeishi}">
            <input name="mid" th:field="*{mid}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">景点封面：</label>
                <div class="col-sm-8">
                    <div class="row" style="height: auto;cursor: pointer;border:1px gainsboro solid;border-radius: 10px 10px;text-align: center" id="upload_div">
                        <img src="" alt="" >
                        <span class="vicp-hint">点击上传图片至此处</span>
                    </div>
                    <input type="file" name="file" id="upload_img" style="display: none;"/>
                    <input type="text" name="coverURL" th:field="*{coverURL}" id="coverURL" style="display: none;"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">美食名称：</label>
                <div class="col-sm-8">
                    <input name="mname" th:field="*{mname}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">商家电话：</label>
                <div class="col-sm-8">
                    <input name="telephone" th:field="*{telephone}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">门店地点：</label>
                <div class="col-sm-8">
                    <input name="position" th:field="*{position}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">美食详情：</label>
                <div class="col-sm-8">
                    <input id="detailContent" name="detail" th:field="*{detail}" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />

    <script th:inline="javascript">
        var prefix = ctx + "travel/meishi";
        $("#form-meishi-edit").validate({
            focusCleanup: true
        });

        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height : 192,
                lang : 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#detailContent").val();
            $('#editor').summernote('code', content);

            //回显封面图片
            var coverURL = $("#coverURL").val();
            $("#upload_div").html('<img src="'+coverURL+'" alt="" class="img-responsive img-rounded">' +
                '<img src="" alt="" >\n' +
                '                        <span class="vicp-hint">点击上传替换图片至此处</span>\n');
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }


        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#detailContent").val(sHTML);
                $.operate.save(prefix + "/edit", $('#form-meishi-edit').serialize());

            }
        }



        $("#upload_div").click(function () {//div点击触发inputfile按钮上传文件的功能
            $("#upload_img").click();
        });

        $("#upload_img").change(function () {//选中文件后,异步上传文件并获取上传文件的地址
            var data = new FormData();
            data.append("file", document.getElementById("upload_img").files[0]);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        //异步刷新图片
                        var image_URL=result.fileName
                        $("#coverURL").val(image_URL)
                        $("#upload_div").html('<img src="'+image_URL+'" alt="" class="img-responsive img-rounded">\n');
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            })
        });

    </script>
</body>
</html>
